import {Injectable} from "@angular/core";  
import {Http, Headers, Response} from "@angular/http";  
import { Subject } from 'rxjs';
import 'rxjs/add/operator/toPromise';

import { Plan } from '../pages/plan/plan';
import { Sign } from '../pages/plan/sign';
import { Wish } from '../pages/wish/wish';

 @Injectable()
 export class HabitService {

    public planList : Array<Plan>;
    public wishList : Array<Wish>;
    public changes: Subject<any> = new Subject<any>();

    public wish : Wish;

    public token : string;
    public headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});

    private url =  "http://125.220.157.228:8088/learningresource";
    public getPlanUrl = this.url+"/Habit/getPlan";
    public addPlanUrl = this.url+"/Habit/addPlan";
    public updatePlanUrl =  this.url+"/Habit/updatePlan";
    public deletePlanUrl =  this.url+"/Habit/deletePlan/";

    public getWishUrl =  this.url+"/Habit/getWish";
    public addWishUrl =  this.url+"/Habit/addWish";

    public getSignUrl =  this.url+"/Habit/getSign/";
    public signUrl =  this.url+"/Habit/sign/";

    constructor(private http : Http) {
      this.planList = [];
      this.wishList = [];
    }


    addPlan(plan : Plan) : Promise<Response>{
    console.log(plan.time);
    console.log("totalNum:"+ plan.totalNum);
    return this.http
      .post(this.addPlanUrl,{pID:plan.pID, pName:plan.pName, description: plan.description, signedNum : plan.signedNum, totalNum : plan.totalNum, bell : plan.bell, wName : plan.wName, hour : 4, minute : 5})
      .toPromise()
    }

    updatePlan(plan : Plan) : Promise<void>{
    return this.http
      .post(this.updatePlanUrl, {p_name:plan.pName, description: plan.description, signed_num : plan.signedNum, total_num : plan.totalNum, pID : plan.pID})
      .toPromise()
      .then((response:Response)=>{ console.log(response)});
    }
    deletePlan(plan : Plan) : Promise<Response>{
      return this.http
      .delete(this.deletePlanUrl+plan.pID)
      .toPromise();
    }
    getPlan(): Promise<Plan[]> {  
    return this.http.get(this.getPlanUrl).toPromise()  
      .then(this.extractData)  
      .catch(this.handleError);  
    }  

    addWish() : Promise<Response>{
      return this.http
        .post(this.addWishUrl,{wID:this.wishList.length + 1, wName: null, p1 : null, p2 : null, p3 : null})
        .toPromise()
    }

    getWish(): Promise<Wish[]> {  
    return this.http.get(this.getWishUrl).toPromise()  
      .then(this.extractData)  
      .catch(this.handleError);  
    }  

    getSign(pID : number): Promise<Sign[]> {  
    return this.http.get(this.getSignUrl+pID).toPromise()  
      .then(this.extractData)  
      .catch(this.handleError);  
    }  

    sign(pID : number, day : number, signed_num : number) : Promise<void>{
    return this.http
      .get(this.signUrl+pID+"/"+day+"/"+signed_num)
      .toPromise()
      .then((response:Response)=>{});
    }

  private extractData(res: Response) {  
    let body = res.json();  
    //将拿到的数据进行打印....  
    //console.log(body);
  
    return body;  
  }  
  private handleError (error: Response | any) {  
    console.log("有错误???");  
    // In a real world app, we might use a remote logging infrastructure  
    let errMsg: string;  
    if (error instanceof Response) {  
      const body = error.json() || '';  
      const err = body.error || JSON.stringify(body);  
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;  
    } else {  
      errMsg = error.message ? error.message : error.toString();  
    }  
    console.error(errMsg);  
    return Promise.reject(errMsg);  
  }  
 }